package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class TryEx extends JFrame {

	private JPanel contentPane;
	private JTextField su1;
	private JTextField su2;
	private JTextField result;
	private JButton btnNewButton;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JButton btnNewButton_1;
	private JTextField result2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
	
					TryEx frame = new TryEx();
					frame.setVisible(true);
				
			
		
	}

	/**
	 * Create the frame.
	 */
	public TryEx() {
		setTitle("예외처리연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 390, 389);
		contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getSu1());
		contentPane.add(getSu2());
		contentPane.add(getResult());
		contentPane.add(getBtnNewButton());
		contentPane.add(getLblNewLabel());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getResult2());
	}
	private JTextField getSu1() {
		if (su1 == null) {
			su1 = new JTextField();
			su1.setBounds(69, 32, 116, 21);
			su1.setColumns(10);
		}
		return su1;
	}
	private JTextField getSu2() {
		if (su2 == null) {
			su2 = new JTextField();
			su2.setColumns(10);
			su2.setBounds(69, 73, 116, 21);
		}
		return su2;
	}
	private JTextField getResult() {
		if (result == null) {
			result = new JTextField();
			result.setColumns(10);
			result.setBounds(69, 116, 208, 21);
		}
		return result;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("나눗셈");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				try{
					compute();
					}catch(Exception ex){
						result.setText("문자로 나누었거나  0으로 나눔");
					}finally{
						result2.setText("무조건이야~~~~");
					}
			}
			});
			btnNewButton.setBounds(204, 35, 73, 43);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("결과");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(0, 119, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("갯수");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1.setBounds(0, 76, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("피갯수");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_2.setBounds(0, 35, 57, 15);
		}
		return lblNewLabel_2;
	}
	public void compute() throws Exception{
		double s1 = Double.parseDouble(su1.getText());
		double s2 = Double.parseDouble(su2.getText());
		
		double r = s1/s2;
		
		result.setText("" + r); //""공백을 넣으면 숫자가 문자화가 된다
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("예외만들기");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Exception e = new Exception("김환태가 만든 예외 메세지");
					try{
						throw e;
					}catch(Exception ex){
						result2.setText(ex.toString());
					}
				}
			});
			btnNewButton_1.setBounds(49, 147, 286, 21);
		}
		return btnNewButton_1;
	}
	private JTextField getResult2() {
		if (result2 == null) {
			result2 = new JTextField();
			result2.setBounds(49, 178, 286, 147);
			result2.setColumns(10);
		}
		return result2;
	}
}
